<?php

class User_favorite extends CI_Model {

    var $user_id = '';
    var $fb_books = '';
    var $fb_movies = '';
    var $fb_music = '';
    var $app_books = '';
    var $app_movies = '';
    var $app_music = '';
    var $favorite_order_books = '';

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function setAttribute($userid, $fb_books, $fb_movies, $fb_music, $app_books = '', $app_movies = '', $app_music = '', $favorite_order = '') {
        $this->user_id = $userid; // please read the below note
        $this->fb_books = $fb_books;
        $this->fb_movies = $fb_movies;
        $this->fb_music = $fb_music;
        $this->app_books = $app_books;
        $this->app_movies = $app_movies;
        $this->app_music = $app_music;
        $this->favorite_order_books = $favorite_order->favorite_order_books;
        $this->favorite_order_movies = $favorite_order->favorite_order_movies;
        $this->favorite_order_music = $favorite_order->favorite_order_music;
    }

    function getFavorite($user_id) {
        $this->db->select('*');
        $this->db->from('user_favorite');
        $this->db->where('user_id', $user_id);
        $result = $this->db->get();
        return $result->result();
    }

    function getFavoriteHistory() {
        $this->db->select('*');
        $this->db->from('favorite_history');
        $data = $this->db->get()->result();
        $result = array();
        foreach($data as $item){
            $result[] = $item->fb_favorite;
        }
        return $result;
    }

    function getFavoriteOrder($userid) {
        $result = $this->db->get_where('user_favorite', array('user_id' => $userid));
        $data = $result->result();
        if(count($data)>0){
            return $data[0];
        }
        else{
            $data = new stdClass();
            $data->favorite_order_books = "";
            $data->favorite_order_movies = "";
            $data->favorite_order_music = "";
            return $data;
        }
        
    }

    function update_user($userid, $fb_books, $fb_movies, $fb_music, $app_books = '', $app_movies = '', $app_music = '', $favorite_order = '') {
        if ($favorite_order == '') {
            $favorite_order = $this->getFavoriteOrder($userid);
            if ($favorite_order->favorite_order_books == "") {
                $favorite_order->favorite_order_books = $fb_books . "," . $app_books;
            }
            if ($favorite_order->favorite_order_movies == "") {
                $favorite_order->favorite_order_movies = $fb_movies . "," . $app_movies;
            }
            if ($favorite_order->favorite_order_music == "") {
                $favorite_order->favorite_order_music = $fb_music . "," . $app_music;
            }
            if ($app_books == "") {
                $app_books = $favorite_order->app_books;
            }
            if ($app_movies == "") {
                $app_movies = $favorite_order->app_movies;
            }
            if ($app_music == "") {
                $app_music = $favorite_order->app_music;
            }
        }
        $this->setAttribute($userid, $fb_books, $fb_movies, $fb_music, $app_books, $app_movies, $app_music, $favorite_order);
        $this->db->where('user_id', $userid);
        $this->db->update('user_favorite', $this);
    }

    function insertFavoriteChange($fb_favorite_change) {
        $FavoriteHistory = $this->getFavoriteHistory();
        //print_r($FavoriteHistory);
        foreach ($fb_favorite_change as $item) {
            if ($item != "") {
                $dataFavorite = explode(";", $item);
                if (!in_array($dataFavorite[0], $FavoriteHistory)) {
                    $this->db->insert('favorite_history', array("fb_favorite" => $dataFavorite[0], "app_favorite" => $dataFavorite[1]));
                } else {
                    $this->db->where('fb_favorite', $dataFavorite[0]);
                    $this->db->update('favorite_history', array("fb_favorite" => $dataFavorite[0], "app_favorite" => $dataFavorite[1]));
                }
            }
        }
    }

    function insert_user($userid, $fb_books, $fb_movies, $fb_music, $app_books = '', $app_movies = '', $app_music = '') {
        $favorite_order = $this->getFavoriteOrder($userid);
        if ($favorite_order->favorite_order_books == "") {
            $favorite_order->favorite_order_books = $fb_books . "," . $app_books;
        }
        if ($favorite_order->favorite_order_movies == "") {
            $favorite_order->favorite_order_movies = $fb_movies . "," . $app_movies;
        }
        if ($favorite_order->favorite_order_music == "") {
            $favorite_order->favorite_order_music = $fb_music . "," . $app_music;
        }
        $this->setAttribute($userid, $fb_books, $fb_movies, $fb_music, $app_books, $app_movies, $app_music, $favorite_order);
        $this->db->insert('user_favorite', $this);
    }

}